package server.messageContent;

import common.SerializationManager;
import common.data.ResponseTypeEnum;
import common.interfaces.MessageContent;

public class CreateAccountReplyMsgContent implements MessageContent{
	private byte[] data;
	final String RESPONSE_CONTENT = "Created new account with ID ";

	public CreateAccountReplyMsgContent(long accId) {
		//response type
		ResponseTypeEnum responseTypeEnum = ResponseTypeEnum.SUCCESSFUL;
		byte[] responseTypeEnumStream = SerializationManager.getCurrent().fromResponseTypeEnum(responseTypeEnum);
		
		//response message
		String responseString = RESPONSE_CONTENT.concat(String.valueOf(accId));
		byte[] responseStream = SerializationManager.getCurrent()
				.fromString(responseString);
		byte[] responseStreamLength = SerializationManager.getCurrent()
				.fromInt(responseStream.length);
		
		//response object
		byte[] responseObjectStream = SerializationManager.getCurrent().fromLong(accId);
		byte[] responseObjectLengthStream = SerializationManager.getCurrent().fromInt(responseObjectStream.length);
		
		//create response
		data = new byte[responseTypeEnumStream.length
		                + responseStreamLength.length 
		                + responseStream.length
		                + responseObjectLengthStream.length
		                + responseObjectStream.length];
		int currentIndex = 0;
		System.arraycopy(responseTypeEnumStream, 0, data, currentIndex, responseTypeEnumStream.length);
		currentIndex += responseTypeEnumStream.length;
		
		System.arraycopy(responseStreamLength, 0, data, currentIndex, responseStreamLength.length);
		currentIndex += responseStreamLength.length;
		
		System.arraycopy(responseStream, 0, data, currentIndex, responseStream.length);
		currentIndex += responseStream.length;
		
		System.arraycopy(responseObjectLengthStream, 0, data, currentIndex, responseObjectLengthStream.length);
		currentIndex += responseObjectLengthStream.length;
		
		System.arraycopy(responseObjectStream, 0, data, currentIndex, responseObjectStream.length);
		currentIndex += responseObjectStream.length;
	}

	public byte[] getByte() {
		return this.data;
	}

	public int getLength() {
		return this.data.length;
	}
}
